package v1

import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	"pq/models"
	"pq/service"
	"strconv"
)

type CsTestLibController struct {
	TokenCheckController
}

func (ctc *CsTestLibController) GetForBack() {
	limit, _ := ctc.GetInt("limit", models.MAXROWS)
	skip, _ := ctc.GetInt("skip", 0)
	cid, _ := ctc.GetInt("category_id", 1)
	spirit, _ := ctc.GetInt("is_spirit", 0)
	testlib := new(models.PqCsTestLibrary)
	var cate models.PqTestCategory
	slave := models.GetSlaveDb()
	_ = slave.QueryTable("pq_test_category").Filter("id", cid).One(&cate)
	tests, count := testlib.ListAll(-1, spirit, cate.Lft, cate.Rgt, skip, limit)
	ctc.Response(tests, count)
}

func (ctc *CsTestLibController) GetForFront() {
	slave := models.GetSlaveDb()
	//us := service.UserService{}
	cs := service.TestCategoryService{}
	uid, _ := strconv.Atoi(ctc.Uid)
	//user,err := us.GetUserById(uid)
	limit, _ := ctc.GetInt("limit", models.APPROWS)
	skip, _ := ctc.GetInt("skip", 0)
	//beego.Warning(user.PqTestCategoryId)
	var user models.PqUser
	err := slave.QueryTable("pq_user").Filter("id", uid).One(&user)
	if err != nil {
		ctc.Fail(err.Error(), "fail")
	}
	cate, err := cs.GetCategoryById(user.PqTestCategoryId)
	if err != nil {
		ctc.Fail(err.Error(), "fail")
	}
	testlib := new(models.PqCsTestLibrary)

	//tests,count := testlib.ListAll(-1,0,cate.Lft,cate.Rgt,skip,limit)
	tests, count := testlib.ListForFront(user, cate, skip, limit)
	if len(tests) > 0 {
		ctc.Response(tests, count)
	} else {
		ctc.Response([]string{}, 0)
	}
}

func (ctc *CsTestLibController) GetOne() {
	testlib := new(models.PqCsTestLibrary)
	id := ctc.Ctx.Input.Param(":id")
	node := testlib.GetOne(id)
	ctc.Single(node)
}
func (ctc *CsTestLibController) Post() {
	row := new(models.PqCsTestLibrary)
	row.Id, _ = ctc.GetInt("id", 0)
	row.Title = ctc.GetString("title")
	row.Desc = ctc.GetString("desc")
	row.HardRate, _ = ctc.GetFloat("hard_rate", 1.0)
	row.AveragePoint, _ = ctc.GetInt("average_point", 1)
	row.TestCount, _ = ctc.GetInt("test_count", 1)
	row.IsSpirit, _ = ctc.GetInt("is_spirit", 0)
	row.SpiritStart = ctc.GetString("spirit_start", "2000-01-01 00:00:00")
	row.SpiritEnd = ctc.GetString("spirit_end", "2000-01-01 00:00:00")
	row.PqTestAreaId, _ = ctc.GetInt("test_area_id", 0)
	row.AreaCode = ctc.GetString("area_code", "")
	row.AreaCodes = ctc.GetString("area_codes", "")
	row.ModuleType = 1
	row.Score = 100
	row.State, _ = ctc.GetInt("state", 1)
	row.PqTestCategoryId, _ = ctc.GetInt("pq_test_category_id")
	master := models.GetMasterDb()
	//log.Success("%s,%d",row.Title,row.PqTestCategoryId)
	if row.Id > 0 {
		_, err := master.Update(row)
		beego.Error(err)
	} else {
		row.Id = 0
		ins, _ := master.Insert(row)
		row.Id = int(ins)
	}
	res := map[string]interface{}{}
	res["id"] = row.Id
	ctc.Single(res)
}
func (ctc *CsTestLibController) Del() {
	id := ctc.Ctx.Input.Param(":id")
	master := models.GetMasterDb()
	if _, err := master.QueryTable("pq_cs_test_library").Filter("id", id).Update(orm.Params{
		"state": -1,
	}); err != nil {
		ctc.Fail(err.Error(), "")
		return
	}
	ctc.Success("Deleted Successfully")
}
func (ctc *CsTestLibController) Option() {
	ctc.Fail("Options invoke", "")
}

func (ctc *CsTestLibController) GetMyList() {
	limit, _ := ctc.GetInt("limit", models.APPROWS)
	skip, _ := ctc.GetInt("skip", 0)
	//isDone,_ := ctc.GetInt("is_done",0)
	item := new(models.PqCsSimuLibrary)
	items, count := item.ListMySimuList(ctc.Uid, skip, limit)
	for _, im := range items {
		im["finish_rate"] = item.GetFinishRateBySimuId(im["id"])
	}
	ctc.Response(items, count)
}

func (ctc CsTestLibController) Submit() {
	lib_id := ctc.Ctx.Input.Param(":id")
	lib := new(models.PqCsSimuLibrary)
	lib.Id, _ = strconv.Atoi(lib_id)
	master := models.GetMasterDb()
	_, err := master.QueryTable("pq_cs_simu_library").Filter("id", lib_id).Update(orm.Params{
		"is_done": 1,
	})
	if err != nil {
		beego.Error(err)
		ctc.Fail("Updated fail", "")
	}
	ctc.Success("Updated successfully")
}

func (ctc CsTestLibController) GetMyLastTest() {
	var nodes []orm.Params
	model := models.GetSlaveDb()
	model.Raw(`
		select  ctl.* from pq_cs_simu_library as ctl
		where ctl.state = 1 order by ctl.created_time desc
		limit 1
	`).Values(&nodes)
	node := nodes[0]
	ctc.Single(node)
}
